Importing Pandas


In [ ]:
import pandas as pd

Creating a Series


In [ ]:
series1 = pd.Series([1,2,3])

In [ ]:
series1

In [ ]:
names = pd.Series(['Pedro', 'JoãoR', 'Sam', 'Hugo'])

In [ ]:
names

Creating a DataFrame


In [ ]:
df1 = pd.DataFrame([ [1,2,3], [4,5,6] ])

In [ ]:
df1

In [ ]:
df2 = pd.DataFrame([[1,2,3],[4,5,6]], columns=['col1', 'col2', 'col3'])

In [ ]:
df2

In [ ]:
puns_per_day = pd.Series([72, 47, 4, 5])

In [ ]:
puns_per_day

In [ ]:
df2 = pd.DataFrame([names, puns_per_day])

In [ ]:
df2.head()

In [ ]:
df3 = pd.DataFrame()

In [ ]:
df3

Adding columns to a DataFrame


In [ ]:
df3['names'] = names

In [ ]:
df3

In [ ]:
df3['puns_per_day'] = pd.Series([72, 47, 4, 5])

In [ ]:
df3

In [ ]:
df3['times_fired'] = [930, 48, 750, 355]

In [ ]:
df3

Setting the index


In [ ]:
df3 = df3.set_index('names')

In [ ]:
df3

Resetting the index


In [ ]:
df3 = df3.reset_index()

In [ ]:
df3

In [ ]:
df3 = df3.set_index('times_fired')

In [ ]:
df3

Sorting the index


In [ ]:
df3 = df3.sort_index()

In [ ]:
df3

In [ ]:
df3 = df3.sort_index(ascending=False)

In [ ]:
df3

In [ ]:
df3 = df3.reset_index()

In [ ]:
df3

Reading a DataFrame


In [ ]:
df_avengers = pd.read_csv('../data/avengers.csv')

Previewing a DataFrame


In [ ]:
df_avengers.head()

In [ ]:
df_avengers.tail()

Shape of a DataFrame


In [ ]:
df_avengers.shape

Data types in a DataFrame


In [ ]:
df_avengers.dtypes

Setting the index of a DataFrame


In [ ]:
df_avengers = df_avengers.set_index("Appearances")

In [ ]:
df_avengers.head()

Sorting the index of a DataFrame


In [ ]:
df_avengers = df_avengers.sort_index(ascending=False)

In [ ]:
df_avengers.head()

Resetting the index of a DataFrame


In [ ]:
# Notice how instead of assigning the variable, we use the argument 'inplace=True'
# This modifies the actual DataFrame, instead of returning a copy with the alteration.

df_avengers.reset_index(inplace=True)

In [ ]:
df_avengers.head()

In [ ]: